package com.ling.core.view.builder;

import com.bstek.dorado.view.widget.Control;
import com.bstek.dorado.view.widget.base.toolbar.ToolBar;
import com.ling.core.view.ViewComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author bo.wang
 * @since 2013-2-18
 */
@Component
public class ToolbarBuilder extends AbstractControlBuilder {
    public void build(Object control, ViewComponent parentViewComponent) {
        ToolBar toolbar = (ToolBar) control;
        String id = toolbar.getId();
        ViewComponent component = new ViewComponent();
        parentViewComponent.addChildren(component);
        component.setId(id);
        component.setIcon(">dorado/res/" + ToolBar.class.getName().replaceAll("\\.", "/") + ".png");
        component.setName(ToolBar.class.getSimpleName());
        if (StringUtils.isEmpty(id)) {
            component.setEnabled(false);
        }
        for (Control c : toolbar.getItems()) {
            for (IControlBuilder builder : builders) {
                if (builder.support(c)) {
                    builder.build(c, component);
                    break;
                }
            }
        }
    }

    public boolean support(Object control) {
        return control instanceof ToolBar;
    }

}
